<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
    <title>我的购物车</title>

    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->

    <link rel="stylesheet" href="../js/bootstrap/css/bootstrap.min.css">

    <link rel="stylesheet" type="text/css" href="../js/shop/css/webbase.css" />
    <link rel="stylesheet" type="text/css" href="../js/shop/css/pages-cart.css" />
</head>

<body>
<div id="navDiv"></div>
<!--head-->
<div class="cart py-container" id="cartListDiv">
    <!--All goods-->
    <div class="allgoods">
        <h4>全部商品 <a href="index.html">继续选购</a></h4>

        <div class="cart-main">
            <div class="yui3-g cart-th">
                <div class="yui3-u-1-4"><input type="checkbox" name="states" onclick="pitchs(this)" /> 全部</div>
                <div class="yui3-u-1-4">商品</div>
                <div class="yui3-u-1-8">单价（元）</div>
                <div class="yui3-u-1-8">数量</div>
                <div class="yui3-u-1-8">小计（元）</div>
                <div class="yui3-u-1-8">操作</div>
            </div>
            <div class="cart-item-list">

                <div class="cart-body" id="cartBody">

                </div>

            </div>
        </div>

        <div class="cart-tool">
            <div class="option">
                <a href="#none" onclick="deleteBatch()">删除选中的商品</a>
                <a href="#none">移到我的关注</a>
                <a href="#none">清除下柜商品</a>
            </div>
            <div class="toolbar" id="toolbarDiv">

            </div>
        </div>
        <div class="clearfix"></div>

    </div>
</div>
<!-- 底部栏位 -->
<div style="display: none" id="cartProductDiv">
    <div class="cart-list">
        <ul class="goods-list yui3-g">
            <li class="yui3-u-1-24">
                <input type="checkbox" name="ids" value="##productId##" onclick="state()"/>
            </li>
            <li class="yui3-u-11-24">
                <div class="good-item">
                    <div class="item-img"><img src="##image##" style="width: 50px"/></div>
                    <div class="item-msg">##productName##</div>
                </div>
            </li>

            <li class="yui3-u-1-8"><span class="price">##price##</span></li>
            <li class="yui3-u-1-8">
                <a href="#" class="increment mins" onclick="buy('##productId##',-1)">-</a>
                <input autocomplete="off" type="text" value="##count##" minnum="1" class="itxt" />
                <a href="#" class="increment plus" onclick="buy('##productId##',1)">+</a>
            </li>
            <li class="yui3-u-1-8"><span class="sum">##sumPrice##</span></li>
            <li class="yui3-u-1-8">
                <a href="#none" onclick="deleteCart('##productId##')">删除</a><br />
                <a href="#none">移到我的关注</a>
            </li>
        </ul>
    </div>
</div>

<div style="display: none" id="cartDiv">
    <div class="chosed">已选择<span>##totalCount##</span>件商品</div>
    <div class="sumprice">
        <span><em>总价（不含运费） ：</em><i class="summoney">##totalPrice##</i></span>
        <span><em>已节省：</em><i>-¥20.00</i></span>
    </div>
    <div class="sumbtn">
        <a class="sum-btn" href="getOrderInfo.html">结算</a>
    </div>
</div>


<!--页面底部-->
<div class="clearfix footer">
    <div class="py-container">
        <div class="footlink">
            <div class="Mod-service">
                <ul class="Mod-Service-list">
                    <li class="grid-service-item intro  intro1">

                        <i class="serivce-item fl"></i>
                        <div class="service-text">
                            <h4>正品保障</h4>
                            <p>正品保障，提供发票</p>
                        </div>

                    </li>
                    <li class="grid-service-item  intro intro2">

                        <i class="serivce-item fl"></i>
                        <div class="service-text">
                            <h4>正品保障</h4>
                            <p>正品保障，提供发票</p>
                        </div>

                    </li>
                    <li class="grid-service-item intro  intro3">

                        <i class="serivce-item fl"></i>
                        <div class="service-text">
                            <h4>正品保障</h4>
                            <p>正品保障，提供发票</p>
                        </div>

                    </li>
                    <li class="grid-service-item  intro intro4">

                        <i class="serivce-item fl"></i>
                        <div class="service-text">
                            <h4>正品保障</h4>
                            <p>正品保障，提供发票</p>
                        </div>

                    </li>
                    <li class="grid-service-item intro intro5">

                        <i class="serivce-item fl"></i>
                        <div class="service-text">
                            <h4>正品保障</h4>
                            <p>正品保障，提供发票</p>
                        </div>

                    </li>
                </ul>
            </div>
            <div class="clearfix Mod-list">
                <div class="yui3-g">
                    <div class="yui3-u-1-6">
                        <h4>购物指南</h4>
                        <ul class="unstyled">
                            <li>购物流程</li>
                            <li>会员介绍</li>
                            <li>生活旅行/团购</li>
                            <li>常见问题</li>
                            <li>购物指南</li>
                        </ul>

                    </div>
                    <div class="yui3-u-1-6">
                        <h4>配送方式</h4>
                        <ul class="unstyled">
                            <li>上门自提</li>
                            <li>211限时达</li>
                            <li>配送服务查询</li>
                            <li>配送费收取标准</li>
                            <li>海外配送</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>支付方式</h4>
                        <ul class="unstyled">
                            <li>货到付款</li>
                            <li>在线支付</li>
                            <li>分期付款</li>
                            <li>邮局汇款</li>
                            <li>公司转账</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>售后服务</h4>
                        <ul class="unstyled">
                            <li>售后政策</li>
                            <li>价格保护</li>
                            <li>退款说明</li>
                            <li>返修/退换货</li>
                            <li>取消订单</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>特色服务</h4>
                        <ul class="unstyled">
                            <li>夺宝岛</li>
                            <li>DIY装机</li>
                            <li>延保服务</li>
                            <li>品优购E卡</li>
                            <li>品优购通信</li>
                        </ul>
                    </div>
                    <div class="yui3-u-1-6">
                        <h4>帮助中心</h4>
                    </div>
                </div>
            </div>
            <div class="Mod-copyright">
                <ul class="helpLink">
                    <li>关于我们<span class="space"></span></li>
                    <li>联系我们<span class="space"></span></li>
                    <li>关于我们<span class="space"></span></li>
                    <li>商家入驻<span class="space"></span></li>
                    <li>营销中心<span class="space"></span></li>
                    <li>友情链接<span class="space"></span></li>
                    <li>关于我们<span class="space"></span></li>
                    <li>营销中心<span class="space"></span></li>
                    <li>友情链接<span class="space"></span></li>
                    <li>关于我们</li>
                </ul>
                <p>地址：郑州市福元路和英协路交叉口，华悦时间广场</p>
            </div>
        </div>
    </div>
</div>
<!--页面底部END-->
<script src="../js/jquery-3.3.1.js"></script>
<script src="../js/bootstrap/js/bootstrap.min.js"></script>
<script src="../js/jquery.cookie.min.js"></script>
<script src="../js/common.js"></script>

<script>
    var v_token;
    $(function (){
        v_token = sessionStorage.getItem(TOKEN);
        initCart();
    })

    //全选反选框
    function pitchs(obj){
        var arr = $("input[name=ids]");
        for (var i = 0; i < arr.length; i++) {
            arr[i].checked = obj.checked;
        }
    }
    //复选框状态
    function state(){
        var arr = $("input[name=ids]");
        for (var i = 0; i < arr.length; i++) {
            if (arr[i].checked == false){
                return $("input[name=states]")[0].checked = false
            }
        }
        $("input[name=states]")[0].checked = true
    }
    //批量删除购物车商品
    function deleteBatch(){
        var idArr = [];
        $("input[name=ids]:checkbox:checked").each(function (){
            idArr.push(this.value);
        })
        if (idArr.length>0){
            if (!v_token){
                $("#cartListDiv").html('<h1 align="center">请<a style="color: #003eff" href="login.html">登录</a>后在操作</h1>');
            }
            $.ajax({
                type:"delete",
                url:API_URL+"carts",
                data:{"ids":idArr.join(",")},
                beforeSend:function (xhr){
                    xhr.setRequestHeader(X_AUTH,v_token)
                },
                success:function (res){
                    if (res.code == 200){
                        initCart();
                    }
                }
            })
        }else {
            alert("请至少选择一项");
        }
    }

    //删除购物车商品
    function deleteCart(id){
        if (v_token){
            $.ajax({
                type:"delete",
                url:API_URL+"carts/"+id,
                data:{"id":id},
                beforeSend:function (xhr){
                    xhr.setRequestHeader(X_AUTH,v_token)
                },
                success:function (res){
                    if (res.code == 200){
                        initCart();
                    }
                }
            })
        }else {
            $("#cartListDiv").html('<h1 align="center">请<a style="color: #003eff" href="login.html">登录</a>后在操作</h1>');
        }

    }

    //增加 减少
    function buy(id,count){
        var v_param = {};
        v_param.productId = id;
        v_param.count = count;
        $.ajax({
            type:"post",
            url:API_URL+"carts",
            data: v_param,
            beforeSend:function (xhr){
                xhr.setRequestHeader(X_AUTH,v_token);
            },
            success:function (res){
                if (res.code == 200){
                    initCart();
                }
            }
        })
    }

    //购物车
    function initCart(){
        if (!v_token){
            $("#cartListDiv").html('<h1 align="center">请<a style="color: #003eff" href="login.html">登录</a>后在操作</h1>');
        }
        $.ajax({
            type:"get",
            url:API_URL+"carts",
            beforeSend:function (xhr){
                xhr.setRequestHeader(X_AUTH,v_token);
            },
            success:function (res){
                if (res.code == 200){
                    if (!res.data){
                        $("#cartListDiv").html('<h1 align="center">购物车为空<a style="color: #003eff" href="index.html">现在去购物</a></h1>');
                    }
                    var v_cartList = res.data.cartItemVoList;
                    var v_html = '';
                    for (let v_cart of v_cartList) {
                        v_html += $("#cartProductDiv").html()
                                    .replace(/##image##/g,v_cart.image)
                                    .replace(/##productName##/g,v_cart.productName)
                                    .replace(/##price##/g,v_cart.price)
                                    .replace(/##count##/g,v_cart.count)
                                    .replace(/##sumPrice##/g,v_cart.totalPrice)
                                    .replace(/##productId##/g,v_cart.id);
                    }

                    $("#cartBody").html(v_html);
                    var v_cartDiv = $("#cartDiv").html();
                    var v_cartHtml = v_cartDiv.replace(/##totalCount##/g,res.data.totalCount)
                              .replace(/##totalPrice##/g,res.data.totalSumPrice);
                    $("#toolbarDiv").html(v_cartHtml)
                }else {
                    $("#cartListDiv").html('<h1 align="center">请<a style="color: #003eff" href="login.html">登录</a>后在操作</h1>');
                }
            }
        })
    }
</script>
</body>

</html>